from flask import abort
from flask_restful import Resource, marshal_with, fields, reqparse

from myapp.models import db, User, Typedict, Codedict
from .parsers import typedict_get_parser, codedict_get_parser

code_fields = {
    'id': fields.Integer(),
    'typedict_id': fields.Integer(),
    'name': fields.String(),
    'isUse': fields.Boolean()
}


class CodeAPI(Resource):
    @marshal_with(code_fields)
    def get(self, typeid=None):
        if typeid:
            code = Codedict.query.filter(Codedict.typedict_id==typeid).all()
        else:
            code = Codedict.query.all()
        return code

# class CodesAPI(Resource):
#     regs = codedict_get_parser.parse_args()
#     print(regs)
#
#     @marshal_with(code_fields)
#     def get(self,typedict_id=None):
#         if typedict_id:
#             codes = Codedict.query.get(typedict_id)
#             if not codes:
#                 abort(404)
#             return codes
#         else:
#             args = codedict_get_parser.parse_args()
#             page = args['page'] or 1
#             codes = Codedict.query.all().paginate(page,10)
#             return codes.items
